(function($) {
    var modaldialog = {};
	var obj;//倒计时对象
	var a;//setInterval对象
    var i;//倒计时时间
    var callback={};
	function showDialog(msgx, options) {
        if (!$.inArray(options.type, modaldialog.DialogTypes)) {
            options.type = modaldialog.DialogTypes[0];
        }
        ;var settings = $.extend({
            title: modaldialog.DialogTitles[options.type]
        }, modaldialog.defaults, options);
        settings.timeout = (typeof (settings.timeout) == "undefined") ? 0 : settings.timeout;
        settings.showClose = ((typeof (settings.showClose) == "undefined") | !settings.timeout) ? true : !!settings.showClose;
		
        //console.log("document.getElementById('dialog'):"+document.getElementById('dialog'));
		if (!document.getElementById('dialog')) {
            dialog = document.createElement('div');
            dialog.id = 'dialog';
            $(dialog).html("<div id='dialog-header'>" + "<div id='dialog-title'></div>" + "<div id='dialog-close'></div>" + "</div>" + "<div id='dialog-content'>" + "<div id='dialog-content-inner' />" + "<div id='dialog-button-container'>" + "<input type='button' id='dialog-button' value='Close'>" + "</div>" + "</div>");
            dialogmask = document.createElement('div');
            dialogmask.id = 'dialog-mask';
            $(dialogmask).hide();
            $(dialog).hide();
            document.body.appendChild(dialogmask);
            document.body.appendChild(dialog);
            //$("#dialog-close").click(function(){modaldialog.hide;clearInterval(a);});
            //$("#dialog-button").click(function(){modaldialog.hide;clearInterval(a);});
            $("#dialog-close").click(function(){
                clearInterval(a);
                modaldialog.hide();
                //如果有关闭按钮，则等待关闭按钮按下之后才callback,或倒计时结束回调
                if(typeof(callback) === "function"){
                callback();
            }});
            $("#dialog-button").click(function(){
                clearInterval(a);
                modaldialog.hide();
                //如果有关闭按钮，则等待关闭按钮按下之后才callback,或倒计时结束回调
                if(typeof(callback) === "function"){
                callback();
            }});
            //$("#dialog-button").click(function(){clearInterval(a);modaldialog.hide();}); 
            //$("#dialog-close").click(modaldialog.hide);
            //$("#dialog-button").click(modaldialog.hide); 
            
            
            
        }
        var dl = $('#dialog');
        var dlh = $('#dialog-header');
        var dlc = $('#dialog-content');
        var dlb = $('#dialog-button');
        $('#dialog-title').html(settings.title);
       
        
        //如果设置了timeout时间，就特殊处理
		var msg=msgx;
		if(settings.timeout!= 0)
		{
			var time="<span class='the_seconds'>"+settings.timeout+"</span>"+"s";//倒计时s
            msg=msgx+time;            
		}
        //end
        
         $('#dialog-content-inner').html(msg);
		// //获取倒计时的对象the_seconds
		// obj = $(".the_seconds");
        // i=settings.timeout;		
		// a = setInterval(timeFn, 1000);
        //end
        // //倒计时函数
        // function timeFn() {
        //     i--;
        //     obj.html(i);
        //     console.log("i:"+i);
        //     if (i == 0) {
        //         clearInterval(a);
        //         console.log("callback:"+typeof(callback))
        //         if(typeof(callback) === "function"){//倒计时结束回调往下运行
        //             callback();
        //         }
        //     }
        // };
        // //end

        dl.css('width', settings.width);
        var dialogTop = Math.abs($(window).height() - dl.height()) / 2;
        dl.css('left', ($(window).width() - dl.width()) / 2);
        dl.css('top', (dialogTop >= 25) ? dialogTop : 25);
        $.each(modaldialog.DialogTypes, function() {
			//console.log("this+header:"+this+"header");
            dlh.removeClass(this + "header")
        });
        dlh.addClass(settings.type + "header")
        $.each(modaldialog.DialogTypes, function() {
            dlc.removeClass(this)
        });
        dlc.addClass(settings.type);
        $.each(modaldialog.DialogTypes, function() {
            dlb.removeClass(this + "button")
        });
        dlb.addClass(settings.type + "button")
        if (!settings.showClose) {
            $('#dialog-close').hide();
            $('#dialog-button-container').hide();
        } else {
            $('#dialog-close').show();
            $('#dialog-button-container').show();
        }
        if (settings.timeout) {
            window.setTimeout("$('#dialog').fadeOut('slow', 0); $('#dialog-mask').fadeOut('normal', 0);", (settings.timeout * 1000));
        }
        dl.fadeIn("slow");
        $('#dialog-mask').fadeIn("normal");
    };//showDialog end
    
     //倒计时函数
     function timeFn() {
        i--;
        obj.html(i);
        //console.log("i:"+i);
        if (i == 0) {
            clearInterval(a);
            //console.log("callback:"+typeof(callback))
            if(typeof(callback) === "function"){//倒计时结束回调往下运行
                callback();
            }
        }
    };
    //end

	modaldialog.error = function $$modaldialog$error(msg, options) {
        if (typeof (options) == "undefined") {
            options = {};
        }
        options['type'] = "error";
        callback=options['callback'] ;
        showDialog(msg, options);
        //获取倒计时时间
        i=options['timeout'];	
        if(typeof i == "number")
		    {
                obj = $(".the_seconds");
                a = setInterval(timeFn, 1000);
            }
        //return (showDialog(msg, options));
        return ;
    }
    modaldialog.warning = function $$modaldialog$error(msg, options) {
        if (typeof (options) == "undefined") {
            options = {};
        }
        options['type'] = "warning";
        callback=options['callback'] ;
        showDialog(msg, options);
        //获取倒计时时间
        i=options['timeout'];	
        if(typeof i == "number")
		    {
                obj = $(".the_seconds");
                a = setInterval(timeFn, 1000);
            }
        //return (showDialog(msg, options));
        return ;
    }
    modaldialog.success = function $$modaldialog$error(msg, options) {
        if (typeof (options) == "undefined") {
            options = {};
        }
        options['type'] = "success";
        callback=options['callback'] ;
        showDialog(msg, options);
        //获取倒计时时间
        i=options['timeout'];	
        console.log("type i="+typeof(i));
        if(typeof i == "number")
		    {
                obj = $(".the_seconds");
                a = setInterval(timeFn, 1000);
            }
        //return (showDialog(msg, options));
        return;
    }
    modaldialog.prompt = function $$modaldialog$error(msg, options) {
        if (typeof (options) == "undefined") {
            options = {};
        }
        options['type'] = "prompt";
        callback=options['callback'] ;
        showDialog(msg, options);
        //获取倒计时时间
        i=options['timeout'];	
        if(typeof i == "number")
		    {
                obj = $(".the_seconds");
                a = setInterval(timeFn, 1000);
            }
        //return (showDialog(msg, options));
        return ;
    }
    modaldialog.hide = function $$modaldialog$hide() {
        $('#dialog').fadeOut("slow", function() {
            $(this).hide(0);
        });
        $('#dialog-mask').fadeOut("normal", function() {
            $(this).hide(0);
        });
    }
    ;
    modaldialog.DialogTypes = new Array("error","warning","success","prompt");
    modaldialog.DialogTitles = {
        "error": "!! Error !!",
        "warning": "Warning!",
        "success": "Success",
        "prompt": "Please Choose"
    };
    modaldialog.defaults = {
        timeout: 0,
        showClose: true,
        width: 525
    };
    $.extend({
        modaldialog: modaldialog
    });
}
)(jQuery);
